﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Winsteps.Validator;

public partial class Manage_userManage : System.Web.UI.Page
{
    BLL.RoleBll roleBll = new BLL.RoleBll();
    BLL.UserBll userBll = new BLL.UserBll();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadValidator();

            BindSelect();
            ddl_Role.Items.Add("--角色信息--");
            ddl_Role.Items[ddl_Role.Items.Count - 1].Selected = true;

            BindAllUser();

            //---------------------默认绑定所有的用户信息数据---------------------------------
        }
        
    }

    /// <summary>
    /// 将所有的用户取出
    /// </summary>
    private void BindAllUser()
    {
        DataTable table = userBll.GetUserAll();
        grd_UserList.DataSource = table;
        grd_UserList.DataBind();
    }

    /// <summary>
    /// 根据角色绑定用户信息
    /// </summary>
    /// <param name="roleId"></param>
    private void BindUserByRole(int roleId)
    {
        DataTable table = userBll.GetUserByRole(roleId);
        grd_UserList.DataSource = table;
        grd_UserList.DataBind();
    }

    private void BindSelect()
    {
        DataTable table = roleBll.GetAllRole();
        ddl_Role.DataSource = table;
        ddl_Role.DataBind();
        
    }
    protected void btn_AddUser_Click(object sender, EventArgs e)
    {
        if (CheckSelect())
        {
            lb_SelectOrAdd.Text = "新增用户";
            lb_RoleId.Text = ddl_Role.SelectedValue.ToString();
            lb_Role.Text = ddl_Role.SelectedItem.ToString();
            addUser.Style["display"] = "block";
            userList.Style["display"] = "none";

            initUserList();
        }
        
    }

    //新增时，初试化新增列表
    private void initUserList()
    {
        txt_UserId.Text = "";
        txt_UserName.Text = "";
        txt_UserPwd.Text = "";
        txt_TwoPwd.Text = "";
        RadioButton1.Checked = true;
        RadioButton2.Checked = false;
    }

    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        Model.User user = new Model.User();

        user.roleId = int.Parse(lb_RoleId.Text.ToString());
        user.usrId = txt_UserId.Text.ToString();
        user.usrName = txt_UserName.Text.ToString();
        //user.usrPwd  = txt_UserPwd.Text.ToString();
        user.usrPwd = BLL.Cryptography.MD5(txt_UserPwd.Text.ToString());

        if (RadioButton1.Checked)
        {
            user.usrState = 0;
        }
        else
        {
            user.usrState = 1;
        }

        if (userBll.AddUser(user))
        {
            this.Response.Write(" <script>alert('添加用户成功'); </script> ");
            btn_SelectUser_Click(null, null);
        }
        else
        {
            this.Response.Write(" <script>alert('添加用户失败'); </script> ");
            btn_SelectUser_Click(null, null);
        }
        
    }

    /// <summary>
    /// 检查角色是否被选中
    /// </summary>
    private bool CheckSelect()
    {
        if (ddl_Role.SelectedItem.Text == "--角色信息--")
        {
            this.Response.Write(" <script>alert('请先选择角色'); </script> ");
            return false;
        }
        else
        {
            return true;
        }
    }

    private void LoadValidator()
    {
        
    }

    protected void btn_SelectUser_Click(object sender, EventArgs e)
    {
        if (CheckSelect())
        {
            lb_SelectOrAdd.Text = "用户信息";
            addUser.Style["display"] = "none";
            userList.Style["display"] = "block";
            int roleId = Int32.Parse(ddl_Role.SelectedValue.ToString());
            BindUserByRole(roleId);
        }
    }
    protected void grd_UserList_DataBinding(object sender, EventArgs e)
    {
        
    }

    protected string ShowState(int state)
    {
        if (state == 0)
        {
            return "无效";
        }
        else
        {
            return "有效";
        }
    }
    protected void grd_UserList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //Button btn = (Button)grd_UserList.FindControl("btn_UpdateState");
        //Button btn = (Button)grd_UserList.Row.FindControl("btn_UpdateState");
        //btn.Text = "fjowjfij";
        Button btn = (Button)e.Row.TemplateControl.FindControl("btn_UpdateState");
        //btn.Text = "jsofjow";
    }

    protected string UpdateState(int state)
    {
        if (state == 0)
        {
            return "启用帐号";
        }
        else
        {
            return "禁用账号";
        }
    }
    protected void grd_UserList_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        string userId = grd_UserList.Rows[e.NewSelectedIndex].Cells[0].Text.ToString();
        //Response.Write(userId);
        if (userBll.UpdateUserState(userId))
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "s", "alert('修改成功');", true);
            BindAllUser();
        }
    }
}
